<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;

/* @var $this yii\web\View */
/* @var $model backend\models\Major */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="major-form">

    <?php $form = ActiveForm::begin(); ?>
    <!-- 当前管理员是校级时，默认选择当前学校，且不能更改 -->
    <?= $form->field($model, 'XXID')->dropDownList($this->params["schoolList"] , ['prompt'=>'请选择']); ?>    
    
    <?= $form->field($model, 'BMID')->dropDownList($this->params["deparmentList"] , ['prompt'=>'请选择']) ?>

    <?= $form->field($model, 'ZYDM')->textInput() ?>    
   
    <?= $form->field($model, 'ZYMC')->dropDownList(ArrayHelper::map($this->params['kMajorCatalog'], 'ZYMC', 'ZYMC'), ['prompt'=>'请选择']) ?>

    <?= $form->field($model, 'ZYMLBB')->dropDownList(ArrayHelper::map($this->params['kMajorCatalog'], 'MLBB', 'MLBB'), ['prompt'=>'请选择']) ?>
    
    <?= $form->field($model, 'ZYJC')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'BXZYBH')->textInput(['maxlength' => true]) ?>
    
    <?= $form->field($model, 'PYFA')->fileInput(['class'=>'btn btn-default form-control']) ?>
    
    <?php if(file_exists($model->PYFA)){?>
    <?= Html::a($model->ZYMC."培养方案.".pathinfo($model->PYFA)['extension'], [$model->PYFA], ['style' => 'display:inline-block;margin-left:11.5em;margin-bottom:15px']) ?>
    <?php }?>
    
    <?= $form->field($model, 'ZYJS')->textarea(['rows' => 6]) ?>

    <?= $form->field($model, 'KSSJ')->textInput() ?>

    <?= $form->field($model, 'CXSJ')->textInput() ?>


    <?= $form->field($model, 'ZYLB')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'ZYFX')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'ZSLB')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'XZ')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'JXXS')->dropDownList(['全日制'=>'全日制','非全日制'=>'非全日制'] , ['prompt'=>'请选择']) ?>

    <?= $form->field($model, 'ZXF')->textInput() ?>

    <?= $form->field($model, 'BXXF')->textInput() ?>

    <?= $form->field($model, 'SFGK')->dropDownList([0=>'否',1=>'是']) ?>

    <?= $form->field($model, 'SFSN')->dropDownList([0=>'否',1=>'是']) ?>

    <?= $form->field($model, 'ZSZG')->textInput(['maxlength' => true]) ?>
    
    <?= $form->field($model, 'ZT')->dropDownList(['-1'=>"驳回",'0'=>'待审核','1'=>'已批准'] , ["disabled"=>"disabled"]) ?>
    
    <?php if($model->ZT == -1){?>    
    <?= $form->field($model, 'BHYY')->textInput(["disabled"=>"disabled"]) ?>
    <?php }?>
    
    <div class="form-group">
        <?php if($model->ZT == -1){?>
        <?= Html::submitButton('重新申请', ['class' => 'btn btn-success']) ?>
        <?php }else{?>
        <?= Html::submitButton('保存', ['class' => 'btn btn-success']) ?>
        <?php }?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

<script>
    <?php $this->beginBlock('majorJS'); ?>
    var major = {
		isInit:false,	
		kMajorCatalog:[],//用于专业代码、专业名称、专业目录版本联动下拉框
		init:function(){
			if(this.isInit == true){return;}

			//时间选择
			layui.use('laydate', function(){
				var laydate = layui.laydate;
			  	laydate.render({
			    	elem: '#major-kssj',//指定元素
// 			    	value: new Date()
				});

			  	laydate.render({
			    	elem: '#major-cxsj',//指定元素
// 			    	value: new Date()
				});
			});

			
			//学校、学校部门联动
			$("#major-xxid").on("change" , function(){
				var xxid = parseInt($(this).val());//学校ID
				if(!xxid){return}

				major.emptySelect("#major-bmid");//清空院系部门ID下拉框
				var ajaxUrl = "<?= Url::to('/major/list-school-department')?>?xxid="+xxid;//请求的地址
				$.ajax({
			        "type"  : "GET",
			        "url"   : ajaxUrl,
			        success : function(data) {
				        //填学校院系部门ID
			        	major.fillSelect("#major-bmid" , data);
			        },
			        error:function(err){
				    	major.alert(err.responseText)
				    	major.fillSelect("#major-bmid" , []);
				    }
			    });
		    })
		    
		    
		    //填写了专业代码后，专业名称联动
		    $("#major-zydm").on("change" , function(){
				var zydm = $(this).val().trim();//学校ID				
				if(!zydm){return}

				major.kMajorCatalog = []
				major.emptySelect("#major-zymc");//清空专业名称下拉框
				major.emptySelect("#major-zymlbb");//清空专业目录版本下拉框
				
				var ajaxUrl = "<?= Url::to('/major/get-k-major-catalog')?>?zydm="+zydm;//请求的地址
				$.ajax({
			        "type"  : "GET",
			        "url"   : ajaxUrl,
			        success : function(data) {
						if(!data || !data.length){
							major.alert("您填写的专业代码不存在")
							return 
						}
				        
				        major.kMajorCatalog = data
				        var zymcList = {};
				        for(var i=0 ; i<data.length ; i++){
					        var zymc = data[i]['ZYMC']
				        	zymcList[zymc] = zymc
						}
				        major.fillSelect("#major-zymc" , zymcList);
			        },
			        error:function(err){
				    	major.alert(err.responseText)
				    	major.fillSelect("#major-zymc");//清空专业名称下拉框
						major.fillSelect("#major-zymlbb");//清空专业目录版本下拉框
				    }
			    });
		    })
		    
		    //选择专业名称后，专业目录版本联动
    	    $("#major-zymc").on("change" , function(){
    			var zymc = $(this).val()		
    			if(!zymc){return}
    			
    			major.emptySelect("#major-zymlbb");//清空专业目录版本下拉框
				for(var i=0 ; i<major.kMajorCatalog.length ; i++){
					var zymlbbList = {}
					if( zymc == major.kMajorCatalog[i]['ZYMC'] ){
						var zymlbb = major.kMajorCatalog[i]['MLBB'];
						zymlbbList[zymlbb] = zymlbb
					}
				}
				major.fillSelect("#major-zymlbb" , zymlbbList);
    	    })
			this.isInit = true
		},

		//清空下拉选择框并禁用
		emptySelect:function(cssSelector){
			$(cssSelector).attr("disabled" , "disabled")
			$(cssSelector).empty()
		},

		//填充下拉选择框内容
		fillSelect:function(cssSelector , data , selectedValue){
			$(cssSelector).append("<option value=''>请选择</option>")
        	for(var bmid in data){
        		$(cssSelector).append("<option value='"+bmid+"'>"+data[bmid]+"</option>")
		    }
        	
        	$(cssSelector).removeAttr("disabled")
		},

		//alert
		alert:function($content){
			if(window.parent.layer){
				window.parent.layer.alert($content)
			}else{
				alert($content)
			}
		}
   	}

    major.init();

    
    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['majorJS']); ?>
</script>
